package com.example.webview_test.h5_record;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.window.OnBackInvokedDispatcher;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.webview_test.R;

public class MainActivity extends AppCompatActivity {

    private WebView webView;
    
    //xxxxxxxxxxxxxyyyyyyyyyyyyyyyy
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView = findViewById(R.id.webView);
        getNetUrl();
        testLocalURL(); // XXXX

    }

    private void testLocalURL() {
        webView.loadUrl("file:///android_asset/index.html");
        MyWebViewClient webViewClient = new MyWebViewClient();
        webView.setWebViewClient(webViewClient);
        WebSettings webSettings = webView.getSettings();
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);//禁止缓存
        webSettings.setUseWideViewPort(true);//页面自适应
        webSettings.setAllowFileAccess(true);//允许访问文件
        webSettings.setBuiltInZoomControls(true);//显示缩放按钮
        webSettings.setSupportZoom(false);//是否支持缩放
        webSettings.setJavaScriptEnabled(true);//是否支持js交互
        //创建交互类JavaScriptInterface，交互的方法必须添加注解JavascriptInterface
        //<button id="btn1" onclick="window.Android.testClick('我是js传过来的')">按钮1</button>
        webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);//允许页面有http和https混合资源访问
        }
        MyWebChromeClient webChromeClient = new MyWebChromeClient();
        webView.setWebChromeClient(webChromeClient);
    }

    private void getNetUrl() {
        webView.loadUrl("https://www.baidu.com/");
        MyWebViewClient webViewClient = new MyWebViewClient();
        webView.setWebViewClient(webViewClient);
        WebSettings webSettings = webView.getSettings();
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);//禁止缓存
    }


    @SuppressLint("MissingSuperCall")
    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            finish();
        }
    }

    private void initwebSetting(WebSettings webSettings) {
        //如果访问的页面中要与Javascript交互，则webview必须设置支持Javascript
        webSettings.setJavaScriptEnabled(true);
        //设置自适应屏幕，两者合用
        webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
        webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小
        //缩放操作
        webSettings.setSupportZoom(true); //支持缩放，默认为true。是下面那个的前提。
        webSettings.setBuiltInZoomControls(true); //设置内置的缩放控件。若为false，则该WebView不可缩放
        webSettings.setDisplayZoomControls(false); //隐藏原生的缩放控件
        //其他细节操作
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //关闭webview中缓存
        webSettings.setAllowFileAccess(true); //设置可以访问文件
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口
        webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片
        webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式
    }
}